package com.pys.service;

import java.util.List;

import org.hibernate.Session;

import com.pys.dao.impl.RoleDAO;
import com.pys.pojo.PysRole;
import com.pys.utils.HibernateUtil;

public class RoleService {

	public PysRole getById(long id) {
		Session session = null;
		PysRole pysRole = null;
		try {
			session = HibernateUtil.getSessionFactory().openSession();
			RoleDAO roleDAO = new RoleDAO(session);
			pysRole = (PysRole) roleDAO.findById(PysRole.class, id);
		} finally {
			if (session != null) {
				session.close();
			}
		}
		return pysRole;
	}

	public void save(PysRole pysRole) {
		Session session = null;
		try {
			session = HibernateUtil.getSessionFactory().openSession();
			session.beginTransaction();
			RoleDAO roleDAO = new RoleDAO(session);
			roleDAO.save(pysRole);
			session.getTransaction().commit();
		} catch (RuntimeException e) {
			if (session != null) {
				session.getTransaction().rollback();
			}
			throw e;
		} finally {
			if (session != null) {
				session.close();
			}
		}
	}

	public void update(PysRole pysRole) {
		Session session = null;
		try {
			session = HibernateUtil.getSessionFactory().openSession();
			session.beginTransaction();
			RoleDAO roleDAO = new RoleDAO(session);
			roleDAO.update(pysRole);
			session.getTransaction().commit();
		} catch (RuntimeException e) {
			if (session != null) {
				session.getTransaction().rollback();
			}
			throw e;
		} finally {
			if (session != null) {
				session.close();
			}
		}

	}

	// public void roleEkle(String roleAd) {
	// Session session = HibernateUtil.getSessionFactory().openSession();
	// session.beginTransaction();
	// RoleDAO roleDAO = new RoleDAOImpl(session);
	// PysRole pysRole = new PysRole();
	// pysRole.setAd(roleAd);
	// roleDAO.save(pysRole);
	// session.getTransaction().commit();
	// session.close();
	// }

	public List<PysRole> getAllList(Boolean deleted) {
		Session session = null;
		List<PysRole> list = null;
		try {
			session = HibernateUtil.getSessionFactory().openSession();
			RoleDAO roleDAO = new RoleDAO(session);
			if (deleted == null)
				list = roleDAO.findAll(PysRole.class);
			else
				list = roleDAO
						.findByProperty(PysRole.class, "deleted", deleted);
		} finally {
			if (session != null) {
				session.close();
			}
		}
		return list;
	}

	public void delete(PysRole pysRole) {
		Session session = null;
		try {
			session = HibernateUtil.getSessionFactory().openSession();
			session.beginTransaction();
			RoleDAO roleDAO = new RoleDAO(session);
			roleDAO.delete(pysRole);
			session.getTransaction().commit();
		} catch (RuntimeException e) {
			if (session != null) {
				session.getTransaction().rollback();
			}
			throw e;
		} finally {
			if (session != null) {
				session.close();
			}
		}
	}

	public List<PysRole> getByName(String name) {
		Session session = null;
		List<PysRole> list = null;
		try {
			session = HibernateUtil.getSessionFactory().openSession();
			RoleDAO roleDAO = new RoleDAO(session);
			list = roleDAO.findByProperty(PysRole.class, "name", name);
		} finally {
			if (session != null) {
				session.close();
			}
		}
		return list;
	}

}
